package adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Toast;

import com.qianfeng.lingshijia.R;

import com.qianfeng.lingshijia.DetailsActivity;

import java.util.ArrayList;
import java.util.List;

import custom.Commodity;
import custom.Commodity_stockpile;
import entity.HeadEntity;

/**
 * Created by Mr.xingkong on 2016/1/11.
 */
public class StockpileAdapter extends BaseAdapter implements View.OnClickListener {
    List<HeadEntity.DataEntity.StockpileProductListEntity> data;
    Context context;
    int positions = 0 ;
    public StockpileAdapter(Context context)
    {
        this.context = context;
        data = new ArrayList<>();
    }

    /**
     * 设置数据
     * @param data
     */
    public void setData(List<HeadEntity.DataEntity.StockpileProductListEntity> data)
    {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    String picUrl,name,price,message;
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        positions = position;
        ViewHolder viewHolder = null;
        if (convertView == null)
        {
            convertView = new Commodity_stockpile(context);
            viewHolder = new ViewHolder();
            viewHolder.commodity = (Commodity_stockpile) convertView;
            convertView.setTag(viewHolder);
        }else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        picUrl = data.get(position).getImgUrl();
        name = data.get(position).getTitle();
        price = "¥"+data.get(position).getPriceStr();
        message ="¥"+ data.get(position).getOriginalPriceStr();
        convertView.setOnClickListener(this);
        viewHolder.commodity.shoppingCart_iv_stock.setOnClickListener(this);
        viewHolder.commodity.setData(picUrl, name, price, message);

        return convertView;
    }

    @Override
    public void onClick(View v)
    {
        if (v.getId() == R.id.shoppingCart_iv_stock)
        {
            Toast.makeText(context, "放入购物车", Toast.LENGTH_SHORT).show();
        }else{
            Intent intent = new Intent();
            intent.setClass(context, DetailsActivity.class);
            intent.putExtra("productId",data.get(positions).getProductId());
            context.startActivity(intent);
        }
    }

    class ViewHolder
    {
        Commodity_stockpile commodity;
    }
}
